package tools;

/**
 * 对象序列化 反序列化
 * @author chz
 *
 */
import java.io.ByteArrayOutputStream;  
import java.io.IOException;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  
  
import org.hibernate.Hibernate;  
  
/** 
 * @author baoyou E-mail:curiousby@163.com 
 * @version 创建时间：2015年11月3日 下午6:47:56 des: 
 */  
public class HibernateBlobUtil {  
  
    public static java.sql.Blob ObjectToBlob(Object obj)  {  
        try {  
            ByteArrayOutputStream out = new ByteArrayOutputStream();  
            ObjectOutputStream outputStream = new ObjectOutputStream(out);  
            outputStream.writeObject(obj);  
            byte[] bytes = out.toByteArray();  
            outputStream.close();  
            return Hibernate.createBlob(bytes);  
        } catch (Exception e) {  
            return null;  
        }  
  
    }  
  
    public static Object BlobToObject(java.sql.Blob desblob)   {  
        try {  
            Object obj = null;  
            ObjectInputStream in = new ObjectInputStream(  
                    desblob.getBinaryStream());  
            obj = in.readObject();  
            in.close();  
            return obj;  
        } catch (Exception e) {   
            e.printStackTrace();  
        }  
        return null;  
    }  
      
      
}  
